package com.kexio.dynamic.config.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kexio.dynamic.config.entity.ConfigEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 配置数据仓储
 * 
 * 提供配置数据的CRUD操作
 * 
 * @author Kexio
 * @since 1.0.0
 */
@Mapper
public interface ConfigRepository extends BaseMapper<ConfigEntity> {

    /**
     * 根据配置键和环境查询配置
     * 
     * @param configKey   配置键
     * @param environment 环境标识
     * @return 配置实体
     */
    @Select("SELECT * FROM kexio_system_configs WHERE config_key = #{configKey} " +
            "AND environment = #{environment} AND enabled = true AND deleted = 0 " +
            "ORDER BY sort ASC LIMIT 1")
    ConfigEntity findByKeyAndEnvironment(@Param("configKey") String configKey, 
                                       @Param("environment") String environment);

    /**
     * 根据配置分组和环境查询配置列表
     * 
     * @param configGroup 配置分组
     * @param environment 环境标识
     * @return 配置实体列表
     */
    @Select("SELECT * FROM kexio_system_configs WHERE config_group = #{configGroup} " +
            "AND environment = #{environment} AND enabled = true AND deleted = 0 " +
            "ORDER BY sort ASC")
    List<ConfigEntity> findByGroupAndEnvironment(@Param("configGroup") String configGroup, 
                                               @Param("environment") String environment);

    /**
     * 更新配置值
     * 
     * @param configKey   配置键
     * @param configValue 配置值
     * @param environment 环境标识
     * @return 更新行数
     */
    @Update("UPDATE kexio_system_configs SET config_value = #{configValue}, " +
            "updated_time = CURRENT_TIMESTAMP WHERE config_key = #{configKey} " +
            "AND environment = #{environment}")
    int updateConfigValue(@Param("configKey") String configKey, 
                         @Param("configValue") String configValue,
                         @Param("environment") String environment);

    /**
     * 查询所有启用的配置
     * 
     * @param environment 环境标识
     * @return 配置实体列表
     */
    default List<ConfigEntity> findAllEnabled(String environment) {
        QueryWrapper<ConfigEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("environment", environment)
               .eq("enabled", true)
               .eq("deleted", 0)
               .orderByAsc("config_group", "sort");
        return selectList(wrapper);
    }

    /**
     * 根据标签查询配置
     * 
     * @param tags        标签
     * @param environment 环境标识
     * @return 配置实体列表
     */
    default List<ConfigEntity> findByTags(String tags, String environment) {
        QueryWrapper<ConfigEntity> wrapper = new QueryWrapper<>();
        wrapper.like("tags", tags)
               .eq("environment", environment)
               .eq("enabled", true)
               .eq("deleted", 0)
               .orderByAsc("config_group", "sort");
        return selectList(wrapper);
    }
}
